package net.lahuobao.mall.service.impl.admin;

import net.lahuobao.enums.mall.admin.EMallShopSettlementAuditStatus;
import net.lahuobao.mall.dao.admin.MallShopSettlementMapper;
import net.lahuobao.mall.entity.admin.*;
import net.lahuobao.mall.entity.admin.querymodel.MallShopSettlementQueryEntity;
import net.lahuobao.mall.service.admin.MallShopSettlePeriodServiceI;
import net.lahuobao.mall.service.admin.MallShopSettlementServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.DateUtils;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.web.cgform.exception.BusinessException;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by clark on 2016/9/18 0018.
 */
@Service
@Transactional
public class MallShopSettlementServiceImpl extends CommonServiceImpl implements MallShopSettlementServiceI{

    @Autowired
    private MallShopSettlementMapper mapper;

    @Autowired
    private MallShopSettlePeriodServiceI settlePeriodService;

    @Autowired
    private SystemService systemService;

    /**
     * 商户结算列表
     * @param queryEntity
     * @return
     */
    @Override
    public List<MallShopSettlementResultEntity> selectManageShopSettlement(MallShopSettlementQueryEntity queryEntity) {
        return mapper.selectManageShopSettlement(queryEntity, getPageBounds());
    }


    /**
     * 查询商户结算清单
     * @param qeury
     * @return
     */
    @Override
    public List<MallShopSettlementDetailsResultEntity> selectManageShopSettlementDetails(MallShopSettlementQueryEntity qeury) {
        return mapper.selectManageShopSettlementDetails(qeury, getPageBounds());
    }

    /**
     * 确认结算
     * @param shopId
     * @param id
     * @param userId
     */
    @Override
    public void confirmSettlement(String shopId, Integer id, Integer userId) throws Exception{

        String hql = " from MallShopSettlePeriodEntity where shopid = " + shopId;

        //查询商家结算规则
        MallShopSettlePeriodEntity mallShopSettlePeriod = settlePeriodService.singleResult(hql);

        //查询结算
        MallShopSettlementEntity  selectShopSettlementEntity = super.getEntity(MallShopSettlementEntity.class, id);

        systemService.getSession().evict(selectShopSettlementEntity);
        //查询订单
        MallOrderEntity order =  super.getEntity(MallOrderEntity.class, selectShopSettlementEntity.getOrderid());

        MallShopSettlementEntity updateMallShopSettlement = new MallShopSettlementEntity();

        //updateMallShopSettlement.setId(id);
        //设置审核状态
        updateMallShopSettlement.setAuditstatus(EMallShopSettlementAuditStatus.AgreeApplyRefuse.getStatus());
        //设置结算人
        updateMallShopSettlement.setSettlementuserid(userId);
        //设置结算时间
        updateMallShopSettlement.setSettlementtime(new Date());
        //一： 交易总金额
        //计算总金额 = 结算金额 + 交易服务费
       // BigDecimal totalMoney = selectShopSettlementEntity.getSettlementmoney().add(selectShopSettlementEntity.getSettlementexpenses());
        //计算提前结算服务费 = 天数 * 提前结算费/天
        //获取天数的time
        Date now = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        BigDecimal  settleRateAdvanceMoney = null;
        BigDecimal days = null;
            now = format.parse(format.format(now));
            Date settlementTime =  selectShopSettlementEntity.getSettlementtime();
            settlementTime = format.parse(format.format(settlementTime));
            Long daysL = DateUtils.differ(now, settlementTime);
            days = new BigDecimal(daysL);
            if(daysL <= 0){
                throw new BusinessException("已超过可提前结算日期，不能提前结算");
            }else{//计算提前结算服务费
                settleRateAdvanceMoney  = days.multiply(mallShopSettlePeriod.getSettlerateadvance()).multiply(order.getPaymoney());
                //提前结算服务费太大
                if (settleRateAdvanceMoney.doubleValue()>selectShopSettlementEntity.getSettlementmoney().doubleValue()){
                    settleRateAdvanceMoney=selectShopSettlementEntity.getSettlementmoney();
                }
            }
        //计算结算金额 = 结算金额 - 提前结算服务费
        BigDecimal settlementMoney  = selectShopSettlementEntity.getSettlementmoney().subtract(settleRateAdvanceMoney);
        //设置提前结算费
        updateMallShopSettlement.setSettlementexpensesadvance(settleRateAdvanceMoney.setScale(2, BigDecimal.ROUND_HALF_UP));
        //设置结算费
        updateMallShopSettlement.setSettlementmoney(settlementMoney.setScale(2, BigDecimal.ROUND_HALF_UP));
       /* //二： 结算金额
             *//**//*   //计算提前结算服务费 = 金额 * 提前结算费率
                BigDecimal  settleRateAdvanceMoney = resultModel.getSettlementMoney().multiply(mallShopSettlePeriod.getSettleRateAdvance());

                //计算结算金额 = 金额 - 提前结算服务费
                BigDecimal settlementMoney = resultModel.getSettlementMoney().subtract(settleRateAdvanceMoney);
                //设置提前结算费
                updateMallShopSettlement.setSettlementExpensesAdvance(settleRateAdvanceMoney.setScale(2, BigDecimal.ROUND_HALF_UP));
                //设置结算费
                updateMallShopSettlement.setSettlementMoney(settlementMoney.setScale(2, BigDecimal.ROUND_HALF_UP));*//**//**/
        MyBeanUtils.copyBeanNotNull2Bean(updateMallShopSettlement, selectShopSettlementEntity);
        super.saveOrUpdate(selectShopSettlementEntity);
    }

}
